/*
 * Copyright 2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ai.study.dashscope.controller.rag.config;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
// import io.milvus.client.MilvusServiceClient;
// import io.milvus.param.ConnectParam;
// import io.milvus.param.IndexType;
// import io.milvus.param.MetricType;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.embedding.EmbeddingModel;
// import org.springframework.ai.vectorstore.milvus.MilvusVectorStore;
import org.springframework.ai.vectorstore.milvus.MilvusVectorStore;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class MilvusVectorStoreConfiguration {

    @Value("${spring.ai.vectorstore.milvus.client.host}")
    private String host;
    @Value("${spring.ai.vectorstore.milvus.client.port}")
    private Integer port;
    @Value("${spring.ai.vectorstore.milvus.client.username}")
    private String username;
    @Value("${spring.ai.vectorstore.milvus.client.password}")
    private String password;

    @Bean
    public MilvusServiceClient milvusServiceClient(){
        return new MilvusServiceClient(
                ConnectParam.newBuilder()
                        .withHost(host)
                        .withPort(port)
                        .withAuthorization(username,password)
                        .build()
        );
    }

    @Bean(name = "milvusVectorStore")
    // @Qualifier("milvusVectorStore")
    public MilvusVectorStore milvusVectorStore(MilvusServiceClient milvusServiceClient,
                                               @Qualifier("dashScopeEmbeddingModel") EmbeddingModel embeddingModel) {
        return MilvusVectorStore.builder(milvusServiceClient, embeddingModel)
                // .collectionName("vector_store")
                // .databaseName("default")
                // .embeddingDimension(1536)
                // .indexType(IndexType.valueOf("IVF_FLAT"))
                // .metricType(MetricType.valueOf("COSINE"))
                .build();
    }
}
